package com.cutter.mobile;

import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class main extends Activity {
	public static final String PREFS_NAME = "CutterMobilePrefs";
	public static final String appversion = "1.0 beta";
	
    /** Called when the activity is first created. */
    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_vga);     
        
        
        
        
        final Button ImageButton01 = (Button)findViewById(R.id.Button01);        
        ImageButton01.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
            	Intent intent = new Intent(main.this, splitfile.class);
            	startActivity(intent);
            }
        });    
        ImageButton01.setEnabled(false);
        Button ImageButton02 = (Button) findViewById(R.id.Button02);
        ImageButton02.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(main.this, mergefiles.class);
            	startActivity(intent);			      
			}
			
		});
        
        Intent intent = new Intent("org.openintents.action.PICK_FILE"); 
		if (!isCallable(intent)) {
			MissingFileManagerMessageBox();
		} else {
			ImageButton01.setEnabled(true);
			ImageButton02.setEnabled(true);
		}
    }
    private boolean isCallable(Intent intent) {
        List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 
                        PackageManager.MATCH_DEFAULT_ONLY);
        return !list.isEmpty();
}
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
    }
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.about1:
        	AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        	String msg = (String) getText(R.string.aboutmsg);
        	String fmsg ="";
        	fmsg=String.format(msg, appversion);
    		alt_bld.setMessage(fmsg);
    		alt_bld.setCancelable(false);
    		alt_bld.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int id) {
    				
    				
    		}
    		});
    		AlertDialog alert = alt_bld.create();
    		// Title for AlertDialog
    		alert.setTitle(getText(R.string.abouttitle));
    		// Icon for AlertDialog
    		alert.setIcon(R.drawable.info48);
    		//alert.setIcon(R.drawable.icon);
    		alert.show();	
            return true;
        case R.id.oifileman:
        	DownloadFileManager();
            return true;
        case R.id.exitbut:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    public int MissingFileManagerMessageBox() {
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setMessage(getText(R.string.nofilemanager));
		alt_bld.setCancelable(false);
		alt_bld.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				DownloadFileManager();
				
		}
		});
		
		
		alt_bld.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {				
			dialog.cancel();
			finish();
			}
			});
		
		
		AlertDialog alert = alt_bld.create();
		// Title for AlertDialog
		alert.setTitle(getText(R.string.warningtitle));
		// Icon for AlertDialog
		alert.setIcon(R.drawable.alert48);
		//alert.setIcon(R.drawable.icon);
		alert.show();				
		return 0;		
	}
    
    public void DownloadFileManager() {
    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setMessage(getText(R.string.downloadfilemanagermessage));
		alt_bld.setCancelable(false);
		alt_bld.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				String url = "http://www.openintents.org/en/node/159";
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(url));
				startActivity(i);
				finish();				
		}
		});
		
		AlertDialog alert = alt_bld.create();
		// Title for AlertDialog
		alert.setTitle(getText(R.string.downloadfilemanagertitle));
		// Icon for AlertDialog
		alert.setIcon(R.drawable.info48);
		//alert.setIcon(R.drawable.icon);
		alert.show();
    }
    
}